package pers.xiaojun.boot.module.system.controller.dept;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pers.xiaojun.boot.common.pojo.CommonResult;
import pers.xiaojun.boot.common.util.BeanUtils;
import pers.xiaojun.boot.module.system.controller.dept.vo.dept.DeptQueryReqVO;
import pers.xiaojun.boot.module.system.controller.dept.vo.dept.DeptRespVO;
import pers.xiaojun.boot.module.system.controller.dept.vo.dept.DeptSaveReqVO;
import pers.xiaojun.boot.module.system.controller.dept.vo.dept.DeptSimpleRespVO;
import pers.xiaojun.boot.module.system.service.dept.DeptService;
import pers.xiaojun.boot.web.core.annotation.AdminApi;

import java.util.List;

/**
 * 部门接口
 *
 * @author xiaojun
 * @since 2025-11-01
 */
@Tag(name = "部门")
@AdminApi
@RestController
@RequestMapping("/system/dept")
@RequiredArgsConstructor
public class DeptController {

    private final DeptService deptService;

    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:dept:create')")
    @Operation(summary = "创建部门")
    public CommonResult<?> createDept(@RequestBody @Valid DeptSaveReqVO createVO) {
        return CommonResult.success(deptService.saveDept(createVO).getId());
    }

    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:dept:update')")
    @Operation(summary = "更新部门")
    @Parameter(name = "id", description = "部门id", required = true)
    public CommonResult<?> updateDept(@PathVariable("id") Long deptId, @RequestBody @Valid DeptSaveReqVO updateVO) {
        updateVO.setId(deptId);
        return CommonResult.success(deptService.updateDept(updateVO).getId());
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:dept:delete')")
    @Operation(summary = "删除部门")
    @Parameter(name = "id", description = "部门id", required = true)
    public CommonResult<?> deleteDept(@PathVariable("id") Long deptId) {
        deptService.removeDeptById(deptId);
        return CommonResult.success();
    }

    @DeleteMapping("/batch/{ids}")
    @PreAuthorize("@ss.hasPermission('system:dept:delete')")
    @Operation(summary = "删除一批部门")
    @Parameter(name = "ids", description = "部门id列表（使用,分隔）", required = true)
    public CommonResult<?> deleteDeptBatch(@PathVariable("ids") List<Long> deptIds) {
        deptService.removeDeptByIds(deptIds);
        return CommonResult.success();
    }

    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    @Operation(summary = "根据部门Id获取部门信息")
    @Parameter(name = "id", description = "部门id", required = true)
    public CommonResult<DeptRespVO> getDeptById(@PathVariable("id") Long deptId) {
        return CommonResult.success(BeanUtil.toBean(deptService.getDeptById(deptId), DeptRespVO.class));
    }

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    @Operation(summary = "获取部门列表")
    public CommonResult<List<DeptRespVO>> getDeptList(@Valid DeptQueryReqVO queryVO) {
        return CommonResult.success(BeanUtils.toBeans(deptService.listDepts(queryVO), DeptRespVO.class));
    }

    @GetMapping("/simple-list")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    @Operation(summary = "获取简要部门列表")
    public CommonResult<List<DeptSimpleRespVO>> getSimpleDeptList() {
        return CommonResult.success(BeanUtils.toBeans(deptService.list(), DeptSimpleRespVO.class));
    }

}
